package com.pearadmin.pro.modules.school.service.impl;

import java.util.List;
import java.util.ArrayList;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pearadmin.pro.common.tools.core.GeneralConvertUtils;
import com.pearadmin.pro.common.web.base.page.PageResponse;
import com.pearadmin.pro.common.web.base.page.Pageable;

import com.pearadmin.pro.modules.school.domain.*;
import com.pearadmin.pro.modules.school.dto.CourseCodeDTO;
import com.pearadmin.pro.modules.school.dto.CourseShiftInformationDTO;
import com.pearadmin.pro.modules.school.param.CourseShiftInformationRequest;
import com.pearadmin.pro.modules.school.repository.CourseShiftInformationRepository;
import com.pearadmin.pro.modules.school.service.CourseHoursService;
import com.pearadmin.pro.modules.school.service.CourseShiftInformationService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

/**
 * 课程班次信息Service业务层处理
 *
 * @author ChenJian
 * @date 2021-08-29
 */
@Service
public class CourseShiftInformationServiceImpl extends ServiceImpl<CourseShiftInformationRepository, CourseShiftInformation> implements CourseShiftInformationService
{
    @Resource
    private CourseShiftInformationRepository courseShiftInformationRepository;
    @Resource
    private CourseHoursService courseHoursService;

    /**
     * 查询课程班次信息
     * @param request
     * @return 课程班次信息 分页集合
     * */
    @Override
    public PageResponse<CourseShiftInformation> page(CourseShiftInformationRequest request)
    {
        LambdaQueryWrapper<CourseShiftInformation> query = new LambdaQueryWrapper<CourseShiftInformation>();
        return Pageable.of(request,(()->  courseShiftInformationRepository.selectList(query)));
    }

    @Override
    public Boolean saveOrUpdate(CourseShiftInformationDTO courseShiftInformation) {
        CourseShiftInformation courseShiftInformationDb = GeneralConvertUtils.conv(courseShiftInformation, CourseShiftInformation.class);
        saveOrUpdate(courseShiftInformationDb);
        if (!ObjectUtils.isEmpty(courseShiftInformation.getCourseHoursList())) {
            List<CourseHours> courseHoursList = courseShiftInformation.getCourseHoursList();
            courseHoursList.stream().forEach(s -> {
                s.setCsiId(courseShiftInformationDb.getId());
            });
            courseHoursService.saveOrUpdateBatch(courseHoursList);
        }
        return true;
    }

    /**
     * 查询课程班次信息
     *
     * @param id 课程班次信息ID
     * @return 课程班次信息
     */
    @Override
    public CourseShiftInformationDTO selectCourseShiftInformationById(Long id)
    {
        CourseShiftInformationDTO request = GeneralConvertUtils.conv(courseShiftInformationRepository.selectById(id), CourseShiftInformationDTO.class);

        LambdaQueryWrapper<CourseHours> query = new LambdaQueryWrapper<CourseHours>();
        query.eq(CourseHours::getCsiId, id);
        request.setCourseHoursList(courseHoursService.list(query));
        return request;
    }

}
